package com.sefer.dragonfly.client;

import java.util.Iterator;
import java.util.Set;

import javax.management.Attribute;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import com.sefer.dragonfly.client.tools.MonitorLog;
import com.sefer.dragonfly.client.tools.MonitorLogFactory;
import com.sefer.dragonfly.client.tools.MonitorManage;
import com.sefer.dragonfly.client.utils.MBeanAnnotationRegister;

public class CloudMonitorStartup {
	private static MonitorManage monitorManage = null;

	public void start() {
		if (monitorManage == null) {
			monitorManage = MonitorManage.getInstance();
		}
		monitorManage.init();
		monitorManage.start();
	}

	public void stop() {
		if (monitorManage != null) {
			monitorManage.stop();
		}
	}

	// ========================>>>>>>>>>>>>>>>

	public static void main(String[] args) throws MalformedObjectNameException,
			NullPointerException {
		CloudMonitorStartup startUp = new CloudMonitorStartup();
		startUp.start();
		// mbeanList();

		try {
			MBeanAnnotationRegister.addHtmlAdaptorServer(9092);
		} catch (Exception e) {
			e.printStackTrace();
		}

		new Thread() {
			public void run() {
				MonitorLog monitorLog = MonitorLogFactory
						.getLogger("irisPluginCache");
				while (true) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("input data.....................");
					monitorLog.log(new String(""));
				}

			}
		}.start();
	}

	private static void mbeanList() throws MalformedObjectNameException,
			NullPointerException {
		// /================>>>>>>>>>>>>>>>>>>>>>>>
		Set<ObjectName> queryNames = MBeanAnnotationRegister.getMBeanServer()
				.queryNames(new ObjectName("*:*"), null);
		Iterator<ObjectName> iterator = queryNames.iterator();
		MBeanServer mBeanServer = MBeanAnnotationRegister.getMBeanServer();
		System.out
				.println("==========================================================>>>>>>>>>");
		while (iterator.hasNext()) {
			ObjectName oname = iterator.next();

			try {
				MBeanInfo minfo = MBeanAnnotationRegister.getMBeanServer()
						.getMBeanInfo(oname);
				String code = minfo.getClassName();

				if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
					code = oname
							+ ":"
							+ (String) mBeanServer.getAttribute(oname,
									"modelerType");
				}
				if ("javax.management.modelmbean.RequiredModelMBean"
						.equals(code)) {
					code = oname + ":" + minfo.getDescription();
				}
				System.out.println("modelerType: " + code);

				// ================属性============>>>>
				MBeanAttributeInfo attrs[] = minfo.getAttributes();
				Object value = null;

				for (int i = 0; i < attrs.length; i++) {
					if (!attrs[i].isReadable())
						continue;
					String attName = attrs[i].getName();
					if (attName.indexOf("=") >= 0 || attName.indexOf(":") >= 0
							|| attName.indexOf(" ") >= 0) {
						continue;
					}

					try {
						value = mBeanServer.getAttribute(oname, attName);
					} catch (Throwable t) {
						continue;
					}
					if (value == null)
						continue;
					if ("modelerType".equals(attName))
						continue;
					String valueString = value.toString();
					System.out.println("Attribute::" + attName + ": "
							+ valueString);

					if (attrs[i].getName().equals("ruleMemoryValue")) {
						Attribute attr = new Attribute("ruleMemoryValue", 100);
						mBeanServer.setAttribute(oname, attr);

						System.out.println(".......set finished..........");

						Object attribute = mBeanServer.getAttribute(oname,
								"ruleMemoryValue");
						System.out.println("attribute:::: finished:::"
								+ attribute);
					}
				}

				// ===============操作=================>>>
				MBeanOperationInfo[] operations = minfo.getOperations();

				for (int i = 0; i < operations.length; i++) {
					String opName = operations[i].getName();
					System.out.println("operations:::::" + opName + ": "
							+ operations[i].getDescription());
					if ("getState".equals(opName)) {
						System.out.println("method invoke:::"
								+ mBeanServer.invoke(oname, opName,

								new Object[] {}, new String[] {}));
					}
				}

				System.out.println("+++++++++" + "([{ mbeanname:\"" + "123"
						+ "\",methodname:\"" + "123" + "\",value:\"" + value
						+ "\",ip:\"" + "++++++++++++" + "\"}])");

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out
				.println("<<<<<<<<<<<<==========================================================");
	}
}
